package com.feihong.yw189.adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;

import com.feihong.yw189.R;
import com.feihong.yw189.model.Kaika.KaiKaNumInfo;
import com.holenzhou.pullrecyclerview.BaseRecyclerAdapter;
import com.holenzhou.pullrecyclerview.BaseViewHolder;

import java.util.ArrayList;

/**
 * 手机号码数据适配器
 * 2018/1/4.
 */

public class PhoneNumAdapter extends BaseRecyclerAdapter<KaiKaNumInfo.DataBean.ListBean> {

    public PhoneNumAdapter(Context context) {
        super(context,R.layout.item_kaika_num,new ArrayList<KaiKaNumInfo.DataBean.ListBean>());
    }

    @Override
    protected void convert(BaseViewHolder holder, KaiKaNumInfo.DataBean.ListBean item) {

        // 价格/低消
        String price = item.getVirtual_price();
        // 等级
        String num_lev_memo = item.getNum_lev_memo();
        // 预存
        String mini_cost = item.getMini_cost();
        // 地区
        String region_name = item.getRegion_name();
        // 电信3G
        String typeName = item.getType_name();

        // 电话号码
        String phonenum = item.getPhonenum();
        // 增加分隔符
        phonenum = separateString(phonenum, 3, 4, '-');
        SpannableString spannableString = new SpannableString(phonenum);
        ForegroundColorSpan foregroundColorSpan = new ForegroundColorSpan(Color.RED);
        spannableString.setSpan(foregroundColorSpan, 0, 3, Spannable.SPAN_INCLUSIVE_INCLUSIVE);

        holder.setText(R.id.tv_phonenum,spannableString);
        holder.setText(R.id.tv_addtype,region_name);
        holder.setText(R.id.tv_dj_value,"等级" + num_lev_memo);
        holder.setText(R.id.tv_yc_value,"¥  " + price);
        holder.setText(R.id.tv_dx_value,"低消" + mini_cost);
        holder.setText(R.id.tv_gonghao,typeName);
        holder.itemView.setOnClickListener(v ->  mAddPhoneNumItemListener.setPhoneNumItemListener(item));
    }


    /**
     * 用指定字符分隔格式化字符串
     * <br/>（如电话号为1391235678 指定startIndex为3，step为4，separator为'-'经过此处理后的结果为 <br/> 139-1234-5678）
     *
     * @param source     需要分隔的字符串
     * @param startIndex 开始分隔的索引号
     * @param step       步长
     * @param separator  指定的分隔符
     * @return 返回分隔格式化处理后的结果字符串
     */
    private String separateString(String source, int startIndex, int step, char separator) {
        int times = 0;
        StringBuilder tmpBuilder = new StringBuilder(source);
        for (int i = 0; i < tmpBuilder.length(); i++) {
            if (i == startIndex + step * times + times) {
                //if(i == 3 || i == 8){
                if (separator != tmpBuilder.charAt(i)) {
                    tmpBuilder.insert(i, separator);
                }
                times++;
            } else {
                if (separator == tmpBuilder.charAt(i)) {
                    tmpBuilder.deleteCharAt(i);
                    i = -1;
                    times = 0;
                }
            }
        }
        return tmpBuilder.toString();
    }

    private AddPhoneNumItemListener mAddPhoneNumItemListener;

    public void setItemSelectListener(AddPhoneNumItemListener addPhoneNumItemListener) {
        mAddPhoneNumItemListener = addPhoneNumItemListener;
    }

    public interface AddPhoneNumItemListener {

        public void setPhoneNumItemListener(KaiKaNumInfo.DataBean.ListBean phoneData);

        public void setCallPhoneNumListener(String callPhone);
    }
}
